<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人姓名" label-width="auto" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入负责人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:mallShopUser:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:mallShopUser:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:mallShopUser:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:mallShopUser:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="mallShopUserList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <!-- <el-table-column label="商家用户id" align="center" prop="id" /> -->
      <!-- <el-table-column label="用户" align="center" prop="userId" /> -->
      <el-table-column
        label="手机号"
        align="center"
        prop="phone"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="负责人姓名"
        width="100px"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        label="身份证号"
        align="center"
        prop="idcard"
        :show-overflow-tooltip="true"
      ></el-table-column>

      <el-table-column
        label="身份证前照"
        width="100px"
        align="center"
        prop="idcardBefore"
      >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.idcardBefore"
            style="width: 80px"
            :preview-src-list="[scope.row.idcardBefore]"
          /> 
        </template>
      </el-table-column>
      <!-- <el-table-column label="商家LOGO" align="center" prop="img">
        <template slot-scope="scope"> <el-image :src="scope.row.img" style="width: 80px" :preview-src-list="[scope.row.img]"/> </template>
      </el-table-column> -->
      <el-table-column
        label="身份证后照"
        width="100px"
        align="center"
        prop="idcardAfter"
      >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.idcardAfter"
            style="width: 80px"
            :preview-src-list="[scope.row.idcardAfter]"
          /> 
        </template>
      </el-table-column>
      <el-table-column label="营业资质" align="center" prop="imgurl">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.imgurl"
            style="width: 80px"
            :preview-src-list="[scope.row.imgurl]"
          /> </template
        >
      </el-table-column>
      <el-table-column
        label="行业类别"
        align="center"
        prop="industryType"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="行业属性"
        align="center"
        prop="industryAttribute"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="注册号"
        align="center"
        prop="num"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="执照名"
        align="center"
        prop="licenseName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="法人名称"
        align="center"
        prop="legalName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="法人手机号"
        width="100px"
        align="center"
        prop="legalPhone"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="法人身份前照"
        width="100px"
        align="center"
        prop="legalIdcardBefore"
      >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.legalIdcardBefore"
            style="width: 80px"
            :preview-src-list="[scope.row.legalIdcardBefore]"
          /> </template
        >
      </el-table-column>
      <el-table-column
        label="法人身份后照"
        width="100px"
        align="center"
        prop="legalIdcardAfter"
      >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.legalIdcardAfter"
            style="width: 80px"
            :preview-src-list="[scope.row.legalIdcardAfter]"
          /> </template
        >
      </el-table-column>
      <el-table-column
        label="商家名字"
        align="center"
        prop="shopName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="省"
        align="center"
        prop="province"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="市"
        align="center"
        prop="city"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="区"
        align="center"
        prop="area"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="详细地址"
        align="center"
        prop="address"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="商家简介" align="center" prop="intro" :show-overflow-tooltip="true"/>
      <el-table-column label="门店logo" align="center" prop="logo" > <template slot-scope="scope"> <el-image :src="scope.row.logo" style="width: 80px" :preview-src-list="[scope.row.logo]"/> </template> </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="caAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.caAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="upAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="upAt" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.disabled == 0 ? "正常" : "冻结" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <!-- <router-link :to="'/shop/shopUser/handleUpdate/' + scope.row.id" class="link-type">
            <span>商家账号</span>
          </router-link>< size="mini"
            type="text"
            icon="el-icon-add"
            '/users/shopUser/' + scope.row.id"
            -->
        <template slot-scope="scope">
          <router-link
            :to="{ path: '/users/shopUser', query: { shopId: scope.row.id } }"
            class="link-type"
          >
            <span>商家账号</span>
          </router-link>

          <!-- @click="handleUpdate1("queryShopUserByShopId(scope.row)"
            v-hasPermi="['shop:shopUser:query']"
         > 商家账号 </el-button>   -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:mallShopUser:remove']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallShopUser:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- <div style="display:none;">
      <shopUser ref="shopuser"></shopUser>
    </div> -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!-- <el-form-item label="用户id" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择用户id">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            maxlength="11"
          />
        </el-form-item>
        <!-- <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item> -->
        <el-form-item label="负责人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身份证前照">
          <imageUpload v-model="form.idcardBefore" />
        </el-form-item>
        <el-form-item label="身份证后照">
          <imageUpload v-model="form.idcardAfter" />
        </el-form-item>
        <el-form-item label="营业资质">
          <imageUpload v-model="form.imgurl" />
        </el-form-item>
        <!-- <el-form-item label="行业类别" prop="industryType">
          <el-select v-model="form.industryType" placeholder="请选择行业类别">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="行业属性" prop="industryAttribute">
          <el-input v-model="form.industryAttribute" placeholder="请输入行业属性" />
        </el-form-item> -->
        <el-form-item label="注册号" prop="num">
          <el-input v-model="form.num" placeholder="请输入注册号" />
        </el-form-item>
        <el-form-item label="执照名" prop="licenseName">
          <el-input v-model="form.licenseName" placeholder="请输入执照名" />
        </el-form-item>
        <el-form-item label="法人名称" prop="legalName">
          <el-input v-model="form.legalName" placeholder="请输入法人名称" />
        </el-form-item>
        <el-form-item label="法人手机号" prop="legalPhone">
          <el-input v-model="form.legalPhone" placeholder="请输入法人手机号" />
        </el-form-item>
        <el-form-item label="法人身份前照">
          <imageUpload v-model="form.legalIdcardBefore" />
        </el-form-item>
        <el-form-item label="法人身份后照">
          <imageUpload v-model="form.legalIdcardAfter" />
        </el-form-item>
        <el-form-item label="商家名字" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入商家名字" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <!-- <el-input v-model="form.province" placeholder="请输入省" /> -->

          <el-select
            ref="refSheng"
            v-model="form.province"
            @change="changeCity"
            placeholder="请选择省"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="市" prop="city">
          <!-- <el-input v-model="form.city" placeholder="请输入市" /> -->

          <el-select
            v-model="form.city"
            @change="changeCityTo"
            placeholder="请选择市"
            style="width: 100%"
          >
            <el-option
              v-for="item in cityArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="区" prop="area">
          <!-- <el-input v-model="form.area" placeholder="请输入区" /> -->

          <el-select
            v-model="form.area"
            @change="changeCityThree"
            placeholder="请选择区"
            style="width: 100%"
          >
            <el-option
              v-for="item in areaArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="商家简介" prop="intro">
          <el-input
            v-model="form.intro"
            placeholder="请输入商家简介"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="默认/自营">
          <el-radio-group v-model="form.tag">
            <el-radio
              v-for="dict in tagOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.disabled">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">冻结</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMallShopUser,
  getMallShopUser,
  delMallShopUser,
  addMallShopUser,
  updateMallShopUser,
  exportMallShopUser,
} from "@/api/mall/mallShopUser";
import ImageUpload from "@/components/ImageUpload";
import arr from "@/utils/districts.js";
//import { listMallShopUser, getMallShopUser, delMallShopUser, addMallShopUser, updateMallShopUser, exportMallShopUser } from "@/api/mall/mallShopUser";
//import { queryShopUserByShopId } from "@/api/shop/shopUser";

//import shopUser from '@/views/shop/shopUser/index';
export default {
  name: "MallShopUser",
  components: {
    //shopUser,
    ImageUpload,
  },
  data() {
    return {
      value: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家入驻信息表格数据
      mallShopUserList: [],
      // 0审核中1通过2拒绝字典
      statusOptions: [],
      // 0默认1平台自营字典
      tagOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "change" },
        ],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "负责人姓名不能为空", trigger: "blur" },
        ],
        idcard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
        ],
        idcardBefore: [
          { required: true, message: "身份证前照不能为空", trigger: "blur" },
        ],
        idcardAfter: [
          { required: true, message: "身份证后照不能为空", trigger: "blur" },
        ],
        imgurl: [
          { required: true, message: "营业资质不能为空", trigger: "blur" },
        ],
        industryType: [
          { required: true, message: "行业类别不能为空", trigger: "change" },
        ],
        industryAttribute: [
          { required: true, message: "行业属性不能为空", trigger: "blur" },
        ],
        num: [{ required: true, message: "注册号不能为空", trigger: "blur" }],
        licenseName: [
          { required: true, message: "执照名不能为空", trigger: "blur" },
        ],
        legalName: [
          { required: true, message: "法人名称不能为空", trigger: "blur" },
        ],
        legalPhone: [
          { required: true, message: "法人手机号不能为空", trigger: "blur" },
        ],
        legalIdcardBefore: [
          { required: true, message: "法人身份前照不能为空", trigger: "blur" },
        ],
        legalIdcardAfter: [
          { required: true, message: "法人身份后照不能为空", trigger: "blur" },
        ],
        shopName: [
          { required: true, message: "商家名字不能为空", trigger: "blur" },
        ],
        province: [{ required: true, message: "省不能为空", trigger: "blur" }],
        city: [{ required: true, message: "市不能为空", trigger: "blur" }],
        area: [{ required: true, message: "区不能为空", trigger: "blur" }],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "0审核中1通过2拒绝不能为空",
            trigger: "blur",
          },
        ],
        tag: [
          {
            required: true,
            message: "0默认1平台自营不能为空",
            trigger: "blur",
          },
        ],
        caAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        delFlag: [
          { required: true, message: "0默认1删除不能为空", trigger: "blur" },
        ],
      },

      options: [], // 省
      selectedOptions: [],
      cityArr: [], // 城市列表
      areaArr: [], // 区县列表
    };
  },
  created() {
    this.getDicts("mall_shop_shen").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("mall_shop_zi").then((response) => {
      this.tagOptions = response.data;
    });

    this.getList();
    this.initDistPicker();
  },
  methods: {
    // 初始化选择器
    initDistPicker() {
      let distData = arr.arr;
      let options = []; // 省
      for (var provinceKy in distData["100000"]) {
        let optProvinceItem = {
          value: provinceKy,
          label: distData["100000"][provinceKy],
        };
        if (distData[provinceKy]) {
          // 市
          for (var cityKy in distData[provinceKy]) {
            optProvinceItem.children = optProvinceItem.children
              ? optProvinceItem.children
              : [];
            let optCityItem = {
              value: cityKy,
              label: distData[provinceKy][cityKy],
            };
            if (distData[cityKy]) {
              // 区
              for (var areaKy in distData[cityKy]) {
                optCityItem.children = optCityItem.children
                  ? optCityItem.children
                  : [];
                let optAreaItem = {
                  value: areaKy,
                  label: distData[cityKy][areaKy],
                };
                optCityItem.children.push(optAreaItem);
              }
            }
            optProvinceItem.children.push(optCityItem);
          }
        }
        options.push(optProvinceItem);
      }
      this.distData = distData;
      this.options = options;
      // console.log(this.distData);
      console.log(this.options);
    },

    changeCity(value) {
      this.options.map((item, index) => {
        if (item.value == value) {
          console.log(item);
          this.cityArr = item.children; //存储城市列表
          console.log(this.cityArr);
          this.form.province = item.label;
        }
      });
      this.changeCityTo();
    },

    changeCityTo(value) {
      //获取市名
      this.cityArr.map((item, index) => {
        if (item.value == value) {
          console.log(item);
          this.areaArr = item.children; //存储区县列表
          this.form.city = item.label;
          console.log(this.areaArr);
        }
      });
      this.changeCityThree();
    },
    changeCityThree(value) {
      //获取区县名
      this.areaArr.map((item, index) => {
        if (item.value == value) {
          this.form.area = item.label;
        }
      });

      console.log(this.form);
    },

    /** 查询商家入驻信息列表 */
    getList() {
      this.loading = true;
      listMallShopUser(this.queryParams).then((response) => {
        this.mallShopUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        phone: null,
        password: null,
        name: null,
        idcard: null,
        idcardBefore: null,
        idcardAfter: null,
        imgurl: null,
        industryType: null,
        industryAttribute: null,
        num: null,
        licenseName: null,
        legalName: null,
        legalPhone: null,
        legalIdcardBefore: null,
        legalIdcardAfter: null,
        shopName: null,
        province: null,
        city: null,
        area: null,
        address: null,
        intro: null,
        mobile: null,
        times: null,
        scale: null,
        serverId: null,
        logo: null,
        status: 0,
        tag: null,
        caAt: null,
        upAt: null,
        delFlag: null,
        disabled: null,
      };
      this.cityArr = [];
      this.areaArr = [];
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 0审核中1通过2拒绝字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 0默认1平台自营字典翻译
    tagFormat(row, column) {
      return this.selectDictLabel(this.tagOptions, row.tag);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家入驻信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMallShopUser(id).then((response) => {
        console.log(response);
        this.form = response.data;
        this.open = true;
        this.title = "修改商家入驻信息";
      });
    },

    /** 获取商家账号信息 */
    // handleQueryShopUser(row) {
    //   // queryShopUserByShopId(row.id).then(response => {
    //   //    //this.$refs.shopuser.queryShopUserByShopId(response)
    //   //  console.log(row)
    //   //  })

    //   const id = row.id || this.ids
    //   queryShopUser(id).then(response => {
    //     this.$refs.shopuser.handleUpdate1(response)

    //   });

    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMallShopUser(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallShopUser(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除商家入驻信息选中数据", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delMallShopUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有商家入驻信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportMallShopUser(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
